package xkp.lessons;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

public class TestList {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new MyThread();
        Thread thread2 = new MyThread();
        Thread thread3 = new MyThread();
        thread1.start();
        thread2.start();
        thread3.start();
        thread1.join();
        thread2.join();
        thread3.join();
        System.out.println(MyThread.getSize());
    }
}

class MyThread extends Thread{
    //ArrayList非线程安全
    //private static List<String> list = new ArrayList<>();
    //Vector性能稍差
    //private static Vector<String> list = new Vector<>();
    //并发集合
    private static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();

    @Override
    public void run() {
        for(int i = 1; i <= 100; i++){
            list.add(""+i);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int getSize(){
        return list.size();
    }
}
